import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/model/integral_rank.dart';
import 'package:kxy_flutter_app/r.dart';

class UserRankItemWidget extends StatelessWidget {
  final IntegralRankItem rankItem;
  final bool isLight;

  const UserRankItemWidget({Key key, this.rankItem, this.isLight = false}) : super(key: key);

  Widget index() {
    return Container(
      width: 40,
      child: Text(
        rankItem.rank > 0 ? rankItem.rank.toString() : '-',
        style: TextStyle(
          fontSize: 16,
          color: isLight ? Colors.white : Colors.black38,
          fontWeight: isLight ? FontWeight.bold : FontWeight.normal,
        ),
      ),
    );
  }

  Widget avatar() {
    var size = 56.0;
    return Container(
      margin: EdgeInsets.only(right: 15),
      decoration: BoxDecoration(
        color: Colors.transparent,
        shape: BoxShape.circle,
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(100),
        child: CachedNetworkImage(
          width: size,
          height: size,
          fit: BoxFit.cover,
          imageUrl: rankItem.headUrl ?? '',
          errorWidget: (a, b, c) => Image.asset(
            R.assetsImgLogo,
            width: size,
            height: size,
          ),
          placeholder: (x, y) => Image.asset(
            R.assetsImgLogo,
            width: size,
            height: size,
          ),
        ),
      ),
    );
  }

  Widget name() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        Text(
          rankItem.realName,
          style: TextStyle(
            fontSize: 16,
            color: isLight ? Colors.white : Colors.black,
            fontWeight: isLight ? FontWeight.bold : FontWeight.normal,
          ),
        ),
        Text(
          rankItem.deptName,
          style: TextStyle(
            fontSize: 12,
            color: isLight ? Colors.white70 : Colors.black54,
            fontWeight: isLight ? FontWeight.bold : FontWeight.normal,
          ),
        ),
      ],
    );
  }

  Widget grade() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        Text(
          rankItem.grade.toString(),
          style: TextStyle(
            fontSize: 16,
            color: isLight ? Colors.amberAccent : Colors.amber,
            fontWeight: isLight ? FontWeight.bold : FontWeight.normal,
          ),
        ),
        Text(
          '分',
          style: TextStyle(
            fontSize: 10,
            color: isLight ? Colors.amberAccent : Colors.amber,
            fontWeight: isLight ? FontWeight.bold : FontWeight.normal,
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 76,
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            children: [
              index(),
              avatar(),
            ],
          ),
          Expanded(child: name()),
          grade(),
        ],
      ),
    );
  }
}
